package com.inns.position.hbase.coprocessor.client;

import com.inns.position.hbase.HbaseConsts;
import com.inns.position.hbase.bean.StatisticsResult;
import com.inns.position.hbase.coprocessor.service.ObserverStatisticsProtos;
import com.inns.position.hbase.coprocessor.service.ObserverStatisticsProtos.StatisticsService;
import com.inns.position.hbase.util.StatisticsUtil;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.client.coprocessor.Batch;
import org.apache.hadoop.hbase.ipc.BlockingRpcCallback;
import org.apache.hadoop.hbase.ipc.ServerRpcController;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class ObserverStatisticsClient {

    public void test331(Table table) throws Throwable {
        ObserverStatisticsProtos.Request.Builder builder = ObserverStatisticsProtos.Request.newBuilder();
        builder.addGroupColumns(HbaseConsts.IMSICell);
        builder.addCountColumns(HbaseConsts.xCell);

        ObserverStatisticsProtos.Request request = builder.build();

        Map<byte[], List<StatisticsResult>> listMap = table.coprocessorService(StatisticsService.class, null, null, new Batch.Call<StatisticsService, List<StatisticsResult>>() {
            @Override
            public List<StatisticsResult> call(StatisticsService instance) throws IOException {
                ServerRpcController rpcController = new ServerRpcController();
                BlockingRpcCallback<ObserverStatisticsProtos.Response> rpcCallback = new BlockingRpcCallback<>();
                instance.getStatistics331(rpcController, request, rpcCallback);

                ObserverStatisticsProtos.Response response = rpcCallback.get();
                if (rpcController.failedOnException()) {
                    throw rpcController.getFailedOn();
                }
                List<ObserverStatisticsProtos.ExpandRow> resultsList = response.getResultsList();
                List<StatisticsResult> list = new ArrayList<>();
                return list;
            }
        });
        for (List<StatisticsResult> list : listMap.values()) {
            StatisticsUtil.printResultList(list);
        }
    }
}
